package net.maku.generator.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import net.maku.generator.common.page.PageResult;
import net.maku.generator.query.GenCodeIssueQuery;
import net.maku.generator.common.service.impl.BaseServiceImpl;
import net.maku.generator.dao.GenCodeIssueDao;
import net.maku.generator.entity.GenCodeIssueEntity;
import net.maku.generator.service.GenCodeIssueService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 代码问题管理
 *
 * @author 代码小子 crudboy@163.com
 * @since 1.0.0 2024-11-22
 */
@Service
public class GenCodeIssueServiceImpl extends BaseServiceImpl<GenCodeIssueDao, GenCodeIssueEntity> implements GenCodeIssueService {

    @Override
    public PageResult<GenCodeIssueEntity> page(GenCodeIssueQuery query) {
        IPage<GenCodeIssueEntity> page = baseMapper.selectPage(
                getPage(query), getWrapper(query)
        );

        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<GenCodeIssueEntity> getList() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean save(GenCodeIssueEntity entity) {
        entity.setCreateTime(new Date());
        return super.save(entity);
    }

    private LambdaQueryWrapper<GenCodeIssueEntity> getWrapper(GenCodeIssueQuery query){
        LambdaQueryWrapper<GenCodeIssueEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getIssueName()), GenCodeIssueEntity::getIssueName, query.getIssueName());
        wrapper.ge(StringUtils.checkValNotNull(query.getUrgentLevel()), GenCodeIssueEntity::getUrgentLevel, query.getUrgentLevel());
        wrapper.ge(StringUtils.checkValNotNull(query.getResolveStatus()), GenCodeIssueEntity::getResolveStatus, query.getResolveStatus());
        return wrapper;
    }
}